package com.shop.loan.handler;

import com.alibaba.fastjson.JSONObject;
import com.shop.loan.model.common.Code;
import com.shop.loan.model.common.ResResult;
import com.shop.loan.model.vo.WithdrawApplyVo;
import com.shop.loan.rentaldata.RentalPayUtil;
import org.springframework.stereotype.Component;
import xyz.erupt.annotation.fun.OperationHandler;

import javax.annotation.Resource;
import java.util.List;

@Component
public class WithdrawApplyHandler implements OperationHandler<Void, WithdrawApplyVo> {

    @Resource
    RentalPayUtil rentalPayUtil;

    @Override
    public String exec(List<Void> data, WithdrawApplyVo withdrawApplyVo, String[] param) {
        if (withdrawApplyVo.getNumber() != null && withdrawApplyVo.getNumber().doubleValue() <= 0) {
            return "msg.error('金额不能<=0')";
        }
        ResResult withdrawRes = rentalPayUtil.withdrawApply(withdrawApplyVo.getNumber(), withdrawApplyVo.getType());
        if (withdrawRes.getCode().intValue() != Code.SUCCESS.getCode()) {
            return "msg.error('" + withdrawRes.getMsg() + "')";
        }
        return null;
    }

    @Override
    public WithdrawApplyVo eruptFormValue(List<Void> data, WithdrawApplyVo withdrawApplyVo, String[] param) {
        JSONObject balanceJs = rentalPayUtil.getBalance();
        if (balanceJs != null && balanceJs.containsKey("assetBalance")) {
            withdrawApplyVo.setAssetBalance(balanceJs.getBigDecimal("assetBalance"));
        }
        if (balanceJs != null && balanceJs.containsKey("rdBalance")) {
            withdrawApplyVo.setRdBalance(balanceJs.getBigDecimal("rdBalance"));
        }
        return OperationHandler.super.eruptFormValue(data, withdrawApplyVo, param);
    }
}
